# -*- coding: utf-8 -*-

from flask import Flask, request, session, g, redirect, url_for, abort, \
     render_template, flash
import oursql

DEBUG = True

app = Flask(__name__)
app.config.from_object(__name__)


def isUser(username,password):
    conn = oursql.connect(host='127.0.0.1',user='root', passwd='123456',db='bridgeit')
    c = conn.cursor()
    c.execute("SELECT COUNT(*) FROM bridgeit.`user` WHERE username= '"+username+"'AND password='"+password+"';")
    res = c.fetchone()
    c.close()
    return res[0]

@app.route('/')
def login1():
    return render_template('login.html')



@app.route('/login', methods=['GET', 'POST'])
def login():
    error = None
    if request.method == 'POST':
        i = isUser(request.form['username'],request.form['password'])
        if i == 0:
            flash('You were not logged in')
        else:
            session['logged_in']= True
            flash('You were logged in')
            return render_template('login.html')
    return render_template('login.html', error=error)

if __name__ == '__main__':
    app.run()
	
